package com.weiyun.service.impl;

import com.weiyun.mapper.*;
import com.weiyun.pojo.*;
import com.weiyun.service.BusinessService;
import com.weiyun.service.CartService;
import com.weiyun.service.FoodService;
import com.weiyun.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/***
 *   @Author:weiyun
 *
 */
@Service
public class OrderServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private OrderDetailetService orderDetailetService;
    @Autowired
    private BusinessService businessService;
    @Autowired
    private FoodService foodMapper;
    @Autowired
    private CartService cartMapper;
    @Override
    public int createOrders(Orders orders) throws Exception {
        // 订单的日期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String orderDateStr = simpleDateFormat.format(new Date());
        orders.setOrderDate(orderDateStr);
        // 订单的状态，默认为0，表示未支付
        orders.setOrderState(0);
        // 保存订单的基本信息到数据库
         ordersMapper.createOrders(orders);
        // 保存订单的详情
        Cart cart = new Cart();
        cart.setBusinessId(orders.getBusinessId());
        cart.setUserId(orders.getUserId());
        List<Cart> cartList = cartMapper.listCart(cart);
        List<OrderDetailet> list = new ArrayList<>();
        for (Cart c : cartList) {
            OrderDetailet orderDetailet = new OrderDetailet();
            orderDetailet.setOrderId(orders.getOrderId());
            orderDetailet.setFoodId(c.getFoodId());
            orderDetailet.setQuantity(c.getQuantity());
            list.add(orderDetailet);
        }
        // 批量保存订单的详情
        orderDetailetService.saveOrderDetailetBatch(list);
        for (Cart c : cartList) {
            cartMapper.removeCart(c);
        }
        return orders.getOrderId();
    }

    @Override
    public Orders getOrdersById(Integer orderId) throws Exception {
        Orders orders = ordersMapper.getOrdersById(orderId);
        if(orders!=null){
            Business business = businessService.getBusinessById(orders.getBusinessId());
            orders.setBusiness(business);
            List<OrderDetailet> orderDetailets = orderDetailetService.listOrderDetailetByOrderId(orders.getOrderId());
            for (OrderDetailet orderDetailet : orderDetailets) {
                Integer foodId = orderDetailet.getFoodId();
                Food food = foodMapper.getFoodById(foodId);
                orderDetailet.setFood(food);
            }
            orders.setList(orderDetailets);
        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) throws Exception {
        List<Orders> orders = ordersMapper.listOrdersByUserId(userId);
        for (Orders order : orders) {
            Business business = businessService.getBusinessById(order.getBusinessId());
            order.setBusiness(business);
            List<OrderDetailet> orderDetailets = orderDetailetService.listOrderDetailetByOrderId(order.getOrderId());
            for (OrderDetailet orderDetailet : orderDetailets) {
                Integer foodId = orderDetailet.getFoodId();
                Food food = foodMapper.getFoodById(foodId);
                orderDetailet.setFood(food);
            }
            order.setList(orderDetailets);
        }
        return orders;
    }

    @Override
    public int checkOutOrders(Orders orders) throws Exception {
        orders.setOrderState(1);
        return ordersMapper.checkOutOrders(orders);
    }
}
